package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Permission;
import cn.wolfcode.rbac.query.PageResult;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IPermissionService;
import cn.wolfcode.rbac.util.JsonResult;
import cn.wolfcode.rbac.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/permission")
public class PermissionController {
    
    @Autowired
    private IPermissionService permissionService;

    // 提供一个方法处理查询所有部门数据请求，响应 HTML
    @RequestMapping("/list")
    // qo 要贴注解，@ModelAttribute 将数据添加到模型对象中，等价于 model.addAttribute("attributeName", qo);
    @RequiresPermissions(value={"permission:list","权限列表"},logical= Logical.OR)
    public String list(Model model,@ModelAttribute("qo") QueryObject qo){
        PageInfo result = permissionService.query(qo);
        model.addAttribute("result", result);
        return "permission/list"; // /WEB-INF/views/permission/list3.ftl
    }

   //  不做编辑功能

    @RequestMapping("/delete")
    @RequiresPermissions(value={"permission:delete","权限删除"},logical= Logical.OR)
    public String delete(Long id){
        if (id != null) {
            permissionService.delete(id);
        }
        return "redirect:/permission/list.do";  // http://localhost/permission/list.do
    }

    @RequestMapping("/reload")
    @ResponseBody   // 将返回对象转为 json格式的数据相应给前端（前端发送ajax请求）
    public JsonResult reload(){
        try {
            permissionService.reload();
            return new JsonResult(true,"加载成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"加载失败");
        }
    }
}
